package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookCategoryOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/voc")
public class AdminVocCategoryController {

    @Autowired
    VocCategoryService vocCategoryService;
    @SaAdminCheckPermission("admin:category:search")
    @GetMapping("/category/list")
    public Result<PageDTO<VocCategoryDTO>> getCategoryList(PageRequest pageRequest){

        PageDTO<VocCategoryDTO> bookCategories
                = vocCategoryService.getBookCategories(pageRequest);


        return Result.ok(bookCategories);

    }

    @GetMapping("/category")
    public Result<VocCategoryDTO> getCategory(String id){

        VocCategoryDTO vocCategoryDTO = vocCategoryService.getBookCategory(Long.valueOf(id));

        return Result.ok(vocCategoryDTO);

    }
    @SaAdminCheckPermission("admin:category:add")
    @PostMapping("/category")
    public Result addCategory(@RequestBody VocCategoryCommand vocCategoryCommand){
        vocCategoryService.addBookCategory(vocCategoryCommand);
        return Result.ok();
    }
    @SaAdminCheckPermission("admin:category:update")
    @PutMapping("/category")
    public Result updateCategory(@RequestBody VocCategoryCommand vocCategoryCommand){
        vocCategoryService.updateBookCategory(vocCategoryCommand);
        return Result.ok();
    }
    @SaAdminCheckPermission("admin:category:delete")
    @DeleteMapping("/category/{categoryId}")
    public Result deleteCategory(@PathVariable("categoryId") String categoryId){
        vocCategoryService.deleteBookCategory(Long.valueOf(categoryId));
        return Result.ok();
    }
    @SaAdminCheckPermission("admin:category:batchdelete")
    @DeleteMapping("/categories")
    public Result deleteCategories(@RequestBody BatchDeleteCommand batchDeleteCommand){
        vocCategoryService.deleteBookCategories(batchDeleteCommand);
        return Result.ok();
    }

}
